//
//  PeriodicQueryVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/11.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "PeriodicQueryVc.h"
#import "config.h"
#import "Tools.h"
#import "MBProgressHUD+MJ.h"
#import "UIView+circular.h"




@interface PeriodicQueryVc ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *dataPickViewB;
@property (weak, nonatomic) IBOutlet UIPickerView *dataPickViewE;

@property (nonatomic,strong) NSArray *yearArray,*monthArray,*dayArray;
@property (weak, nonatomic) IBOutlet UIButton *seachBtn;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation PeriodicQueryVc
@synthesize yearArray,monthArray,dayArray,delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    [self showInit];
    
    
}




-(void)initView{
    [super initView];
    
    [self.saveBtn viewToCircular];
    [self.titleView setMianTitle:@"自定义周期查询"];
    [self.titleView.doneBtn removeFromSuperview];
    
    self.dataPickViewB.delegate = self;
    self.dataPickViewE.delegate = self;
    self.dataPickViewB.dataSource = self;
    self.dataPickViewE.dataSource = self;
    
    NSLayoutConstraint *buf1,*buf2;
    for (NSLayoutConstraint *con in self.dataPickViewB.constraints) {
        if(con.firstItem==self.dataPickViewB&&con.firstAttribute==NSLayoutAttributeHeight){
            buf1 = con;
            break;
        }
    }
    
    for (NSLayoutConstraint *con in self.dataPickViewE.constraints) {
        if(con.firstItem==self.dataPickViewE&&con.firstAttribute==NSLayoutAttributeHeight){
            buf2 = con;
            break;
        }
    }
    
    if (isIphone4) {
        buf1.constant = 80;
        buf2.constant = 80;
    }else if (isIphone5){
        buf1.constant = 130;
        buf2.constant = 130;
    }
    
    
    [self.seachBtn setBackgroundImage:[Tools getImgByColor:[UIColor redColor]] forState:UIControlStateNormal];
    
    
    yearArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ProvincesAndCities.plist" ofType:nil]];
    
    NSMutableArray *array = [[NSMutableArray alloc]initWithArray:yearArray];
    [array removeObjectsInRange:NSMakeRange (0, 70)];
    yearArray = array;
    
    
    monthArray = [[yearArray objectAtIndex:0] objectForKey:@"Cities"];
    dayArray = [[monthArray objectAtIndex:0] objectForKey:@"days"];

}




-(void)showInit{
    
    
    [self.dataPickViewB selectRow:1 inComponent:0 animated:NO];
    [self.dataPickViewB selectRow:6 inComponent:1 animated:NO];
    [self.dataPickViewB selectRow:15 inComponent:2 animated:NO];
    
    
    [self.dataPickViewE selectRow:1 inComponent:0 animated:NO];
    [self.dataPickViewE selectRow:6 inComponent:1 animated:NO];
    [self.dataPickViewE selectRow:15 inComponent:2 animated:NO];

    [self.dataPickViewB layoutIfNeeded];
    [self.dataPickViewE layoutIfNeeded];
    [self.view layoutIfNeeded];
}






-(void)btnOnclick:(UIButton*)sender{
    
    if (sender.tag==11) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (IBAction)seachBtnOnClick:(id)sender {
    
    NSInteger yearIndex = 0;
    NSInteger monthIndex = 0;
    NSInteger dayIndex = 0;
    
    yearIndex = [self.dataPickViewB selectedRowInComponent:0];
    monthIndex = [self.dataPickViewB selectedRowInComponent:1];
    dayIndex = [self.dataPickViewB selectedRowInComponent:2];
    
    if (yearIndex<0) {
        yearIndex = 0;
    }
    if (monthIndex<0) {
        monthIndex = 0;
    }
    if (dayIndex<0) {
        dayIndex = 0;
    }
    

    int year = [[[yearArray objectAtIndex:yearIndex] objectForKey:@"State"] intValue];
    int month = [[[monthArray objectAtIndex:monthIndex] objectForKey:@"city"] intValue];
    int day = [[dayArray objectAtIndex:dayIndex] intValue];
    
    
    BirthDay *begin = [BirthDay new];
    begin.year = year;
    begin.month  = month;
    begin.day = day;
    
    
    
    {
        yearIndex = [self.dataPickViewE selectedRowInComponent:0];
        monthIndex = [self.dataPickViewE selectedRowInComponent:1];
        dayIndex = [self.dataPickViewE selectedRowInComponent:2];
        
        if (yearIndex<0) {
            yearIndex = 0;
        }
        if (monthIndex<0) {
            monthIndex = 0;
        }
        if (dayIndex<0) {
            dayIndex = 0;
        }
        
        
        int Y = [[[yearArray objectAtIndex:yearIndex] objectForKey:@"State"] intValue];
        int M = [[[monthArray objectAtIndex:monthIndex] objectForKey:@"city"] intValue];
        int D = [[dayArray objectAtIndex:dayIndex] intValue];
        
        BirthDay *end = [BirthDay new];
        end.year = Y;
        end.month = M;
        end.day = D;
        
        
        if(begin.year>end.year||begin.month>end.month||begin.day>end.day){
        
            [MBProgressHUD showError:@"开始时间不能大于结束时间"];
            
        }else if ([delegate respondsToSelector:@selector(timeSelectedFrom:to:)]) {
            [delegate timeSelectedFrom:begin to:end];
        }

    }
    
    
   

}





#pragma mark PickView delegate
#pragma mark - PickerView lifecycle

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [yearArray count];
            break;
        case 1:
            return [monthArray count];
            break;
            
        case 2:
            return [dayArray count];
            break;
            
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [[yearArray objectAtIndex:row] objectForKey:@"State"];
            break;
        case 1:
            return [[monthArray objectAtIndex:row] objectForKey:@"city"];
            break;
            
        case 2:
            return [dayArray objectAtIndex:row];
            break;
            
        default:
            return nil;
            break;
    }
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,100, 44)];
    label.backgroundColor = [UIColor clearColor];
    [label setTextAlignment:UITextAlignmentCenter];
    label.textColor = UIColorFromRGB(0x333333);
    label.font = [UIFont systemFontOfSize:23];
    
    switch (component) {
        case 0:
            label.text = [[yearArray objectAtIndex:row] objectForKey:@"State"];
            break;
        case 1:
            label.text = [[monthArray objectAtIndex:row] objectForKey:@"city"];
            break;
            
        case 2:
            label.text = [dayArray objectAtIndex:row];
            break;
            
        default:
            label.text = @"";
            break;
    }
    
    
    return label;
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0://改变年
            
            
            break;
        case 1://改变月. 每个月的日期不一样  要更新
            
        {
            NSDictionary  *hel = [monthArray objectAtIndex:row];
            NSArray *he = [hel objectForKey:@"days"];
            dayArray = he;
            [pickerView reloadComponent:2];
        }
            
            
            break;
            
        case 2://改变日
            
            
            break;
        default:
            break;
    }
}


@end
